﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

using VSTalk.Engine.Annotations;
using VSTalk.Engine.Core.ContactInfo;
using VSTalk.Engine.Core.EntitySession;
using VSTalk.Model;
using VSTalk.Tools;

namespace VSTalk.Engine.ViewModel.ContactList.ClientResources
{
    public class InterlocutorSummaryViewModel : IContactDetailsMonitor, IListener, INotifyPropertyChanged
    {
        private readonly IInterlocutorContext _interlocutorContext;

        private Interlocutor _interlocutor;

        private readonly ObservableCollection<InterlocutorResourceItemViewModel> _interlocutorResources = new ObservableCollection<InterlocutorResourceItemViewModel>();
        private BitmapImage _photo;
        private ISubscription _resourcesSub;

        private static ISessionModel SessionModel
        {
            get { return AppServices.Get<ISessionModel>(); }
        }

        private static IContactInfoSevice ContactInfo
        {
            get { return AppServices.Get<IContactInfoSevice>(); }
        }

        public Interlocutor Interlocutor
        {
            get { return _interlocutor; }
        }

        public IInterlocutorContext InterlocutorContext
        {
            get
            {
                return _interlocutorContext;
            }
        }

        public ObservableCollection<InterlocutorResourceItemViewModel> InterlocutorResources
        {
            get
            {
                return _interlocutorResources;
            }
        }

        public BitmapImage Photo
        {
            get { return _photo; }
            set
            {
                _photo = value;
                OnPropertyChanged("Photo");
            }
        }


        public InterlocutorSummaryViewModel(Interlocutor interlocutor)
        {
            _interlocutor = interlocutor;
            _interlocutorContext = SessionModel.GetInterlocutorContext(interlocutor);
        }

        private void CreateResourceItem(IInterlocutorResource item)
        {
            var resourceVM = new InterlocutorResourceItemViewModel(item);
            _interlocutorResources.Add(resourceVM);
        }

        private void RemoveResourceItem(IInterlocutorResource item)
        {
            var toRemove = _interlocutorResources.First(i => i.ResourceItem == item);
           _interlocutorResources.Remove(toRemove);
        }

        public void Set(ContactDetails details)
        {
            if (details == null)
            {
                return;
            }
            if (details.PhotoBin == null)
            {
                return;
            }
            using (var ms = new MemoryStream(details.PhotoBin))
            {
                var p = new BitmapImage();
                p.BeginInit();
                p.StreamSource = ms;
                p.CacheOption = BitmapCacheOption.OnLoad;
                p.EndInit();
                Photo = p;
            }
        }

        public void StartListen()
        {
            var resources = _interlocutorContext.Resources;
            ContactInfo.Subscribe(_interlocutor, this);
            _resourcesSub = resources.HandleCollectionChanges(initialOrAdd: CreateResourceItem, removed: RemoveResourceItem);
        }

        public void StopListen()
        {
            ContactInfo.Unsubscribe(_interlocutor, this);
            _resourcesSub.Unsubscribe();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
